<?php

class Notify_log_model extends CI_Model {

    public $_start_time;

    function __construct() {
        parent::__construct();
        $this->tableName = "notify_log";
    }

    /*
     * 添加
     */

    public function addData($data) {
        $outArr = array();
        if (!empty($data)) {
            $outArr = $this->db->insert($this->tableName, $data);
            $outArr = $this->db->insert_id();
        }
        return $outArr;
    }

    /*
     * 编辑
     */

    public function updateData($paramArr = array()) {
        $optionArr = array(
            "fieldArr" => array(),
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        if (!empty($fieldArr)) {
            $outArr = $this->db->update($this->tableName, $fieldArr, $whereArr);
        }
        return $outArr;
    }

    /*
     * 删除
     */

    public function delData($paramArr = array()) {
        $optionArr = array(
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        $outArr = $this->db->delete($this->tableName, $whereArr);
        return $outArr;
    }

    /*
     * 得到
     */

    public function getData($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 得到条数
     */

    function getDataCount($where = '') {
        $num = 0;
        $sql = "SELECT COUNT(*) AS num FROM " . $this->tableName . " " . $where . " LIMIT 1";
        $query = $this->db->query($sql);
        if ($query) {
            $res = $query->result_array();
            $num = $res[0]["num"];
        }
        return $num;
    }

    public function getDataById($id) {
        $outArr = array();
        $outArr = $this->getData("WHERE id=" . $id);
        if (!empty($outArr)) {
            $outArr = $outArr[0];
        }
        return $outArr;
    }

    /*
     * 状态
     */

    public function type() {
        return [
            1 => ["name" => "银联"],
            2 => ["name" => "cbt公服"],
            3 => ["name" => "心嘉"],
            4 => ["name" => "明邦"],
        ];
    }

    public function log_params() {

        $reuslt = [];
        $params = [];
        if ($this->input->post()) {
            $params["post"] = $this->input->post();
        }
        if ($this->input->get()) {
            $params["get"] = $this->input->get();
        }
        if (!empty($_FILES)) {
            $params["files"] = $_FILES;
        }
        if (isset($_SERVER["HTTP_TOKEN"])) {
            $params["header"]["token"] = $_SERVER["HTTP_TOKEN"];
        }
        $body = file_get_contents("php://input");
        if ($body) {
            $params["requestBody"] = $body;
        }
        //$params["server"] = $_SERVER;
        $d = $this->router->fetch_directory();
        $c = $this->router->fetch_class(); //获取控制器名
        $m = $this->router->fetch_method(); //获取方法名
        $action_name = $d . $c . "/" . $m;

        return ["action_name" => $action_name, "params" => $params];
    }

    public function addLog($api_name = "", $response_data = [], $ext_content = [], $start = 0) {

        $work_code = uniqueId();

        $log_params = $this->log_params();
        $content = [
            "request_data" => $log_params["params"],
            "response_data" => $response_data,
        ];
        $mTime = microtime();
        $mTimes = explode(' ', $mTime);
        $m_time = round($mTimes[0] * 1000000, 0);
        //echo jsonEncode($content);
        $end_time = microtime(true);
        $operate_time = round(($end_time - $this->_start_time), 4);

        $addData = [
            "work_code" => $work_code,
            "action_name" => $log_params["action_name"],
            "api_name" => $api_name,
            "content" => jsonEncode($content),
            "ext_content" => is_array($ext_content) ? jsonEncode($ext_content) : $ext_content,
            "created_at" => date("Y-m-d H:i:s"),
            "m_time" => $m_time,
            "operate_time" => $operate_time,
        ];

        $this->addData($addData);
        //file_put_contents(dirname(__FILE__) . "/../../upfile/notify.txt", date("Y-m-d H:i:s.") . $m_time . "\n", FILE_APPEND);
    }

}
